/*****************************************************************************
 * mqtt_modem.c
 *
 * Copyright (C) 2020 Jeasonvor 1101627719@qq.com
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 ****************************************************************************/

/*****************************************************************************
 * Included Files
 ****************************************************************************/
#include "plum_api.h"
/*****************************************************************************
 * Trace Definitions
 ****************************************************************************/
#define LOG_RUN_LEVEL LOG_LEVEL_INFO
#define LOG_MODULE    "mqtt.modem"
#include "thirdparty/log/log.h"
/*****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/

#define CAL_MQTT_MODEM_M5311  "onemo_m5311"
#define CAL_MQTT_MODEM_AIR720 "luat_air720"

#define CAL_MQTT_MODEM_ADAPTER CAL_MQTT_MODEM_AIR720

/*****************************************************************************
 * Private Types
 ****************************************************************************/

/*****************************************************************************
 * Private Function Prototypes
 ****************************************************************************/

/*****************************************************************************
 * Private Data
 ****************************************************************************/

/*****************************************************************************
 * Public Data
 ****************************************************************************/

PLUM_PRIVATE plum_mqtt_cb dev_cb;
/*****************************************************************************
 * Private Functions
 ****************************************************************************/

PLUM_PRIVATE
plum_void dev_mqtt_msg_handler(plum_s8 *topic, plum_s8 *payload,
                               plum_u16 pyloadlen)
{
    if (dev_cb) {
        plum_mqtt_msg_t msg = {0};
        msg.topic           = topic;
        msg.payload         = payload;
        msg.payloadLen      = pyloadlen;
        dev_cb(MQTT_EVT_MSG, &msg);
    }
}

/*****************************************************************************
 * Public Functions
 ****************************************************************************/

PLUM_PUBLIC
plum_mqtt_client_ref_t plum_cal_mqtt_connect(plum_s8 *client_id, plum_s8 *url,
                                             plum_u32 port, plum_s8 *usr,
                                             plum_s8 *passwd)
{
    plum_mqtt_client_ref_t client = plum_null;

    plum_mqtt_ioctl_con_t param = {0};
    param.clentid               = client_id;
    param.url                   = url;
    param.port                  = port;
    param.usrname               = usr;
    param.password              = passwd;
    int rc =
        plum_device_ioctrl(CAL_MQTT_MODEM_ADAPTER, PLUM_MQTT_IOCTL_CON, &param);
    if (rc) {
        LOG_E("connect mqtt server err,rc:%d", rc);
    }
    else {
        client = (plum_mqtt_client_ref_t)&param;
    }

    return (client);
}

PLUM_PUBLIC
plum_s32 plum_cal_mqtt_disconnect(plum_mqtt_client_ref_t client)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        if (!client) {
            rc = PLUM_ECODE_INVALID;
            LOG_E("client is invalid,rc: %d", rc);
            break;
        }

    } while (0);

    return (rc);
}

PLUM_PUBLIC
plum_s32 plum_cal_mqtt_subscribe(plum_mqtt_client_ref_t client, plum_s8 *topic)
{
    PLUM_UNUSED(client);

    plum_mqtt_ioctl_sub_t param = {0};
    param.topic                 = topic;
    plum_s32 rc =
        plum_device_ioctrl(CAL_MQTT_MODEM_ADAPTER, PLUM_MQTT_IOCTL_SUB, &param);
    if (rc) {
        LOG_E("sub mqtt topic[%s] err,rc:%d", topic, rc);
    }

    return (rc);
}

PLUM_PUBLIC
plum_s32 plum_cal_mqtt_unsubscribe(plum_mqtt_client_ref_t client,
                                   plum_s8 *              topic)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        if (!client) {
            rc = PLUM_ECODE_INVALID;
            LOG_E("client is invalid,rc: %d", rc);
            break;
        }

        if (!topic) {
            rc = PLUM_ECODE_EPARA;
            LOG_E("topic is null,rc: %d", rc);
            break;
        }

    } while (0);

    return (rc);
}

PLUM_PUBLIC
plum_s32 plum_cal_mqtt_publish(plum_mqtt_client_ref_t client,
                               plum_mqtt_msg_t *      msg)
{
    PLUM_UNUSED(client);

    plum_mqtt_ioctl_pub_t param = {0};
    param.topic                 = msg->topic;
    param.payload               = msg->payload;
    plum_s32 rc =
        plum_device_ioctrl(CAL_MQTT_MODEM_ADAPTER, PLUM_MQTT_IOCTL_PUB, &param);
    if (rc) {
        LOG_E("publish mqtt msg err,rc:%d", rc);
    }
    else {
        LOG_D("mqtt pub[%s] :%s", msg->topic, msg->payload);
    }

    return (rc);
}

PLUM_PUBLIC
plum_s32 plum_cal_mqtt_register_callback(plum_mqtt_cb cb)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        if (!cb) {
            LOG_E("param is err");
            rc = PLUM_ECODE_EPARA;
            break;
        }

        dev_cb = cb;

        plum_mqtt_ioctl_msg_t param;
        param.handler = dev_mqtt_msg_handler;
        rc = plum_device_ioctrl(CAL_MQTT_MODEM_ADAPTER, PLUM_MQTT_IOCTL_MSG_CB,
                                &param);
        if (rc) {
            LOG_E("set mqtt msg callback err,rc:%d", rc);
            break;
        }

    } while (0);

    return (rc);
}

PLUM_PUBLIC
plum_void plum_cal_mqtt_loop(plum_void)
{
    plum_mqtt_ioctl_yield_t param = {0};

    plum_device_ioctrl(CAL_MQTT_MODEM_ADAPTER, PLUM_MQTT_IOCTL_YIELD, &param);
}

/****************************************************************************/
/*                                                                          */
/*  End of file.                                                            */
/*                                                                          */
/****************************************************************************/
